package com.fr.chart.core.glyph;

import com.fr.base.core.GraphHelper;
import com.fr.base.core.json.JSONException;
import com.fr.base.core.json.JSONObject;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLableReader;
import com.fr.chart.axis.Axis;
import com.fr.chart.axis.RadarAxis;
import com.fr.chart.axis.TextAttr;
import com.fr.chart.base.equals.Equals;
import com.fr.util.Utils;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.geom.Dimension2D;
import java.awt.geom.GeneralPath;
import java.awt.geom.Line2D;
import java.awt.geom.Line2D.Double;
import java.awt.geom.Point2D;
import java.awt.geom.Point2D.Double;
import java.awt.geom.Rectangle2D;
import java.awt.geom.Rectangle2D.Double;
import java.math.BigDecimal;
import java.text.Format;
import java.util.ArrayList;
import java.util.List;

public class RadarAxisGlyph extends AxisGlyph
{
  private static final long serialVersionUID = -2702639014404531055L;
  public static final String XML_TAG = "RadarAxisGlyph";
  private double D = 0.0D;
  private double radian = 0.0D;
  private int categoryCount;
  private RadarAxis radarAxis;
  private List labelList = new ArrayList();

  public RadarAxisGlyph()
  {
  }

  public RadarAxisGlyph(RadarAxis paramRadarAxis)
  {
    super(paramRadarAxis);
  }

  public void init(Rectangle2D paramRectangle2D)
  {
    setBounds(paramRectangle2D);
    setOrigin(new Point2D.Double(paramRectangle2D.getCenterX(), paramRectangle2D.getCenterY()));
    setD(Math.min(paramRectangle2D.getWidth(), paramRectangle2D.getHeight()));
    setRadian(Math.toRadians(360 / getCategoryCount()));
    setAxisLength(getD() / 2.0D);
  }

  public void initMinMaxValue(double paramDouble1, double paramDouble2)
  {
    if (this.radarAxis.isCustomMinValue())
    {
      this.minValue = this.radarAxis.getMinValue();
      paramDouble1 = this.minValue;
    }
    else
    {
      this.minValue = paramDouble1;
    }
    if (this.radarAxis.isCustomMaxValue())
    {
      this.maxValue = this.radarAxis.getMaxValue();
      paramDouble2 = this.maxValue;
    }
    if (paramDouble2 - paramDouble1 > 0.0D)
    {
      this.tickLength = Axis.calculateIncrement(this.minValue, paramDouble2);
      this.smallTickLength = (this.tickLength / 5.0D);
    }
    if ((paramDouble2 - paramDouble1) % this.tickLength != 0.0D)
      paramDouble2 = paramDouble2 + this.tickLength - (paramDouble2 - paramDouble1) % this.tickLength;
    if (!(this.radarAxis.isCustomMaxValue()))
      this.maxValue = paramDouble2;
    if (this.minValue >= this.maxValue)
      this.maxValue = (this.minValue + 10.0D);
  }

  public Axis getAxis()
  {
    return this.radarAxis;
  }

  public void setAxis(Axis paramAxis)
  {
    if (!(paramAxis instanceof RadarAxis))
      throw new IllegalArgumentException();
    this.radarAxis = ((RadarAxis)paramAxis);
  }

  public void setCategoryCount(int paramInt)
  {
    this.categoryCount = paramInt;
  }

  public int getCategoryCount()
  {
    return this.categoryCount;
  }

  public void setD(double paramDouble)
  {
    this.D = paramDouble;
  }

  public double getD()
  {
    return this.D;
  }

  public void setRadian(double paramDouble)
  {
    this.radian = paramDouble;
  }

  public double getRadian()
  {
    return this.radian;
  }

  public void calculateAxisLengthUnit()
  {
    this.unitLength = (this.axisLength / (getMaxValue() - getMinValue()));
  }

  public Point2D getPoint2D(int paramInt, double paramDouble)
  {
    paramDouble -= getCrossValue();
    return new Point2D.Double(this.originPoint.getX() + Math.cos(this.radian * paramInt + 1.570796326794897D) * paramDouble * this.unitLength, this.originPoint.getY() - Math.sin(this.radian * paramInt + 1.570796326794897D) * paramDouble * this.unitLength);
  }

  protected void drawAxisGrid(Graphics paramGraphics)
  {
    Stroke localStroke;
    Paint localPaint;
    double d;
    Line2D[] arrayOfLine2D;
    int i;
    Graphics2D localGraphics2D = (Graphics2D)paramGraphics;
    if ((getAxis().getSecondGridStyle() != 0) && (this.smallTickLength > 0.0D))
    {
      localStroke = localGraphics2D.getStroke();
      localPaint = localGraphics2D.getPaint();
      localGraphics2D.setStroke(GraphHelper.getStroke(getAxis().getSecondGridStyle()));
      localGraphics2D.setPaint(getAxis().getSecondGridColor());
      d = getCrossValue();
      while (d < getMaxValue())
      {
        arrayOfLine2D = getGridLine(d);
        for (i = 0; i < arrayOfLine2D.length; ++i)
          localGraphics2D.draw(arrayOfLine2D[i]);
        d += this.smallTickLength;
      }
      localGraphics2D.setPaint(localPaint);
      localGraphics2D.setStroke(localStroke);
    }
    if ((getAxis().getMainGridStyle() != 0) && (this.tickLength > 0.0D))
    {
      localStroke = localGraphics2D.getStroke();
      localPaint = localGraphics2D.getPaint();
      localGraphics2D.setStroke(GraphHelper.getStroke(getAxis().getMainGridStyle()));
      localGraphics2D.setPaint(getAxis().getMainGridColor());
      d = getCrossValue();
      while (d <= getMaxValue())
      {
        arrayOfLine2D = getGridLine(d);
        for (i = 0; i < arrayOfLine2D.length; ++i)
          localGraphics2D.draw(arrayOfLine2D[i]);
        d += this.tickLength;
      }
      localGraphics2D.setPaint(localPaint);
      localGraphics2D.setStroke(localStroke);
    }
  }

  protected Line2D[] getGridLine(double paramDouble)
  {
    Line2D[] arrayOfLine2D = new Line2D[getCategoryCount()];
    for (int i = 0; i < getCategoryCount(); ++i)
      arrayOfLine2D[i] = new Line2D.Double(getPoint2D(i, paramDouble), getPoint2D(i + 1, paramDouble));
    return arrayOfLine2D;
  }

  protected void drawAxisLine(Graphics paramGraphics)
  {
    Graphics2D localGraphics2D = (Graphics2D)paramGraphics;
    Paint localPaint = localGraphics2D.getPaint();
    localGraphics2D.setPaint(getAxis().getLineColor());
    Line2D[] arrayOfLine2D = getAxisLines();
    for (int i = 0; (i < arrayOfLine2D.length) && (getAxis().isAxisShow()); ++i)
    {
      GraphHelper.draw(localGraphics2D, arrayOfLine2D[i], getAxis().getLineStyle());
      drawArrow(localGraphics2D, arrayOfLine2D[i].getP1(), arrayOfLine2D[i].getP2());
      if (getAxis().isArrowShow())
        drawCateLabel(localGraphics2D, i, arrayOfLine2D[i].getP2(), 16.0D);
      else
        drawCateLabel(localGraphics2D, i, arrayOfLine2D[i].getP2(), 0.0D);
    }
    drawTicks(localGraphics2D);
    localGraphics2D.setPaint(localPaint);
  }

  public void drawTicks(Graphics paramGraphics)
  {
    for (int i = 0; (i < getCategoryCount()) && (getAxis().isAxisShow()); ++i)
    {
      int j = 0;
      int k = 0;
      if ((getAxis().getLabelNumber() == -1) || (getAxis().getLabelNumber() == 0))
        k = 1;
      else
        k = getAxis().getLabelNumber();
      BigDecimal localBigDecimal1 = new BigDecimal(Double.toString(getCrossValue()));
      BigDecimal localBigDecimal2 = new BigDecimal(Double.toString(this.tickLength));
      BigDecimal localBigDecimal3 = new BigDecimal(Double.toString(k));
      double d3 = getCrossValue();
      while (d3 <= getMaxValue())
      {
        GraphHelper.draw(paramGraphics, getTickLine(i, d3, this.radarAxis.getTickLength(), this.radarAxis.getTickMarkType()));
        if (i == 0)
          drawFirstAxisLabel(paramGraphics, i, localBigDecimal1.doubleValue(), j);
        ++j;
        localBigDecimal1 = localBigDecimal1.add(localBigDecimal2.multiply(localBigDecimal3));
        d3 += this.tickLength * k;
      }
    }
    for (i = 0; (i < getCategoryCount()) && (getAxis().isAxisShow()); ++i)
    {
      double d1 = getCrossValue() + this.smallTickLength;
      while (d1 <= getMaxValue())
      {
        double d2 = (d1 - getCrossValue()) / this.smallTickLength;
        if (d2 != 5.0D)
          GraphHelper.draw(paramGraphics, getTickLine(i, d1, this.radarAxis.getSecTickLength(), this.radarAxis.getSecTickMarkType()));
        d1 += this.smallTickLength;
      }
    }
  }

  private void drawCateLabel(Graphics paramGraphics, int paramInt, Point2D paramPoint2D, double paramDouble)
  {
    Graphics2D localGraphics2D = (Graphics2D)paramGraphics;
    String str = getLabelList().get(paramInt).toString();
    Point2D.Double localDouble = new Point2D.Double(paramPoint2D.getX() + Math.cos(this.radian * paramInt + 1.570796326794897D) * paramDouble, paramPoint2D.getY() - Math.sin(this.radian * paramInt + 1.570796326794897D) * paramDouble);
    Dimension2D localDimension2D = TextGlyph.calculateTextDimension(str, new TextAttr());
    double d1 = paramInt * 360 / getCategoryCount() + 90;
    double d2 = localDimension2D.getWidth();
    double d3 = localDimension2D.getHeight();
    double d4 = localDouble.getY() - 0.5D * d3 * (Math.sin(0.0174532925199433D * d1) + 1.0D);
    double d5 = localDouble.getX() + 0.5D * d2 * (Math.cos(0.0174532925199433D * d1) - 1.0D);
    Rectangle2D.Double localDouble1 = new Rectangle2D.Double(d5, d4, d2, d3);
    TextGlyph.drawLabel(localGraphics2D, str, new TextAttr(), localDouble1);
  }

  protected Line2D getTickLine(int paramInt1, double paramDouble, int paramInt2, int paramInt3)
  {
    Point2D localPoint2D = getPoint2D(paramInt1, paramDouble);
    Point2D.Double localDouble1 = new Point2D.Double(localPoint2D.getX() - paramInt2 / 2.0D * Math.cos(paramInt1 * this.radian), localPoint2D.getY() + paramInt2 / 2.0D * Math.sin(paramInt1 * this.radian));
    Point2D.Double localDouble2 = new Point2D.Double(localPoint2D.getX() + paramInt2 / 2.0D * Math.cos(paramInt1 * this.radian), localPoint2D.getY() - paramInt2 / 2.0D * Math.sin(paramInt1 * this.radian));
    if (paramInt3 == 3)
      return new Line2D.Double(localDouble1, localDouble2);
    if (paramInt3 == 1)
      return new Line2D.Double(localDouble1, localPoint2D);
    if (paramInt3 == 2)
      return new Line2D.Double(localPoint2D, localDouble2);
    return new Line2D.Double(localPoint2D, localPoint2D);
  }

  protected void drawFirstAxisLabel(Graphics paramGraphics, int paramInt1, double paramDouble, int paramInt2)
  {
    Point2D localPoint2D = getPoint2D(paramInt1, paramDouble);
    Format localFormat = getAxis().getFormat();
    String str = null;
    if (localFormat != null)
      str = localFormat.format(Double.valueOf(Utils.doubleToString(paramDouble)));
    else
      str = Utils.doubleToString(paramDouble);
    TextAttr localTextAttr = this.radarAxis.getTextAttr();
    if (localTextAttr == null)
      localTextAttr = new TextAttr();
    Dimension2D localDimension2D = TextGlyph.calculateTextDimension(str, localTextAttr);
    double d = 4 + getAxis().getTickLength() / 2;
    Rectangle2D.Double localDouble = null;
    if (getAxis().getAxisLabelPosition() == 0)
      return;
    if (getAxis().isStagger())
      if (paramInt2 % 2 == 0)
        localDouble = new Rectangle2D.Double(localPoint2D.getX() - localDimension2D.getWidth() - d, localPoint2D.getY() - localDimension2D.getHeight() / 2.0D, localDimension2D.getWidth(), localDimension2D.getHeight());
      else
        localDouble = new Rectangle2D.Double(localPoint2D.getX() - getAxisLabelWidth(), localPoint2D.getY() - localDimension2D.getHeight() / 2.0D, localDimension2D.getWidth(), localDimension2D.getHeight());
    else
      localDouble = new Rectangle2D.Double(localPoint2D.getX() - localDimension2D.getWidth() - d, localPoint2D.getY() - localDimension2D.getHeight() / 2.0D, localDimension2D.getWidth(), localDimension2D.getHeight());
    TextGlyph.drawLabel(paramGraphics, str, localTextAttr, localDouble);
  }

  private Line2D[] getAxisLines()
  {
    Line2D[] arrayOfLine2D = new Line2D[getCategoryCount()];
    for (int i = 0; i < getCategoryCount(); ++i)
      arrayOfLine2D[i] = new Line2D.Double(getPoint2D(i, getCrossValue()), getPoint2D(i, getMaxValue()));
    return arrayOfLine2D;
  }

  public Shape getShape()
  {
    GeneralPath localGeneralPath = new GeneralPath();
    Line2D[] arrayOfLine2D = getAxisLines();
    for (int i = 0; i < arrayOfLine2D.length; ++i)
      localGeneralPath.append(arrayOfLine2D[i], false);
    return localGeneralPath;
  }

  public double getCrossValue()
  {
    return this.minValue;
  }

  public void setLabelList(List paramList)
  {
    this.labelList = paramList;
  }

  public List getLabelList()
  {
    return this.labelList;
  }

  public void addCategoryLabel(Object paramObject)
  {
    this.labelList.add(paramObject);
  }

  protected void drawAxisTitle(Graphics paramGraphics)
  {
  }

  public void readXML(XMLableReader paramXMLableReader)
  {
    super.readXML(paramXMLableReader);
    if (paramXMLableReader.isChildNode())
    {
      String str2;
      String str1 = paramXMLableReader.getTagName();
      if (str1.equals("Attr"))
      {
        if ((str2 = paramXMLableReader.getAttr("minValue")) != null)
          setMinValue(Double.valueOf(str2).doubleValue());
        if ((str2 = paramXMLableReader.getAttr("maxValue")) != null)
          setMaxValue(Double.valueOf(str2).doubleValue());
        if ((str2 = paramXMLableReader.getAttr("D")) != null)
          setD(Double.valueOf(str2).doubleValue());
        if ((str2 = paramXMLableReader.getAttr("radian")) != null)
          setRadian(Double.valueOf(str2).doubleValue());
        if ((str2 = paramXMLableReader.getAttr("count")) != null)
          setCategoryCount(Integer.parseInt(str2));
      }
      else if (str1.equals("Axis"))
      {
        if (((str2 = paramXMLableReader.getAttr("class")) != null) && (str2.endsWith(".RadarAxis")))
          this.radarAxis = ((RadarAxis)paramXMLableReader.readXMLObject(new RadarAxis()));
      }
    }
  }

  public void writeXML(XMLPrintWriter paramXMLPrintWriter)
  {
    paramXMLPrintWriter.startTAG("RadarAxisGlyph");
    super.writeXML(paramXMLPrintWriter);
    paramXMLPrintWriter.startTAG("Attr").attr("D", getD()).attr("radian", getRadian()).attr("count", getCategoryCount()).end();
    if (getAxis() != null)
      getAxis().writeXML(paramXMLPrintWriter);
    paramXMLPrintWriter.end();
  }

  public boolean equals(Object paramObject)
  {
    if (!(paramObject instanceof RadarAxisGlyph))
      return false;
    RadarAxisGlyph localRadarAxisGlyph = (RadarAxisGlyph)paramObject;
    if (!(super.equals(localRadarAxisGlyph)))
      return false;
    if (localRadarAxisGlyph.getD() != getD())
      return false;
    if (localRadarAxisGlyph.getRadian() != getRadian())
      return false;
    if (localRadarAxisGlyph.getCategoryCount() != getCategoryCount())
      return false;
    return (Equals.equals(localRadarAxisGlyph.getAxis(), getAxis()));
  }

  public JSONObject toJSONObject()
    throws JSONException
  {
    JSONObject localJSONObject = super.toJSONObject();
    localJSONObject.put("D", this.D);
    localJSONObject.put("radian", this.radian);
    localJSONObject.put("categoryCount", this.categoryCount);
    if (this.radarAxis != null)
      localJSONObject.put("radarAxis", this.radarAxis.toJSONObject());
    if ((this.labelList != null) && (this.labelList.size() > 0))
      localJSONObject.put("labelList", this.labelList.toArray());
    return localJSONObject;
  }

  public String getJSAxisType()
  {
    return "RadarAxisGlyph";
  }

  public void axisExtends(int paramInt1, int paramInt2)
  {
  }
}